/*
 * Stack.c
 *
 *  Created on: 07.01.2013
 *      Author: phru
 */

#include <malloc.h>
#include "Stack.h"

/**
 * Create stack object
 */
stack_t* createStack(void){
	stack_t* pNewStack = malloc(sizeof(stack_t*));
	pNewStack->pop = pop;
	pNewStack->push = push;
	pNewStack->top = top;
	pNewStack->print = print;
	pNewStack->_current = -1;

	return pNewStack;
}


/**
 * Pop top item from stack
 */
int pop(stack_t* pThis){
	/*Safeguard*/

	if((pThis != NULL) && (pThis->_current >= 0)){
		int value = pThis->_stackSpace[pThis->_current];
		pThis->_current--;
		return value;
	}else{
		return -1;
	}
}

/**
 * Push item on stack
 */
void push(int value, stack_t* pThis){
	if(pThis != NULL){
		pThis->_current++;
		pThis->_stackSpace[pThis->_current] = value;
	}
}

/**
 * Return top item without removing it
 */
int top(stack_t* pThis){
	if(pThis != NULL){
		return -1;
	}
	return pThis->_stackSpace[pThis->_current];
}

/**
 * Print stack content
 */
void print(stack_t* pThis){
	int i;
	for(i = pThis->_current; i >= 0; i--){
		printf("Stack value on position %d is: %d \n",i,pThis->_stackSpace[i]);
	}
}

